In [ ]:
%%HTML
<style>
.container { width:100% }
</style>

Digit Recognition using a Support Vector Machine


In [ ]:
import gzip
import pickle
import random
import numpy       as np
import sklearn.svm as svm

The function $\texttt{load_data}()$ returns a pair of the form $$ (\texttt{training_data}, \texttt{test_data}) $$ where

  • $\texttt{training_data}$ is a list containing 60,000 pairs $(\textbf{x}, y)$ s.t. $\textbf{x}$ is a 784-dimensional `numpy.ndarray` containing the input image and $y$ is the digit that is supposed to be shown in the image $\textbf{x}$.
  • $\texttt{test_data}$ is a list containing 10,000 pairs $(\textbf{x}, y)$. In each case, $\textbf{x}$ is a 784-dimensional `numpy.ndarry` containing the input image, and $y$ is the corresponding digit value.

In [ ]:
def load_data():
    with gzip.open('mnist.pkl.gz', 'rb') as f:
        train, validate, test = pickle.load(f, encoding="latin1")
    X_train = np.array([np.reshape(x, (784, )) for x in train[0]])
    X_test  = np.array([np.reshape(x, (784, )) for x in test [0]])
    Y_train = np.array(train[1])
    Y_test  = np.array(test [1])
    return (X_train, X_test, Y_train, Y_test)

In [ ]:
X_train, X_test, Y_train, Y_test = load_data()

Let us see what we have read:


In [ ]:
X_train.shape, X_test.shape, Y_train.shape, Y_test.shape

We define a support vector machine with Gaussian kernel.


In [ ]:
M = svm.SVC(kernel='rbf', gamma=0.05, C=5)

In [ ]:
%%time
M.fit(X_train, Y_train)

In [ ]:
M.score(X_train, Y_train)

In [ ]:
M.score(X_test, Y_test)

In [ ]: